package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.dao.PictureUserSettingsDAO;
import org.jetnuke.entities.PictureUserSettings;

public class GenericPictureUserSettingsDAO implements PictureUserSettingsDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericPictureUserSettingsDAO.class);

	public PictureUserSettings retrieveByPk(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPk(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("PictureUserSettingsModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		PictureUserSettings s = new PictureUserSettings();

		if (rs.next()) {
			fillPictureUserSettingsFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPk(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	private void fillPictureUserSettingsFromResultSet(PictureUserSettings s,
			ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillPictureUserSettingsFromResultSet(PictureUserSettings, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setDESCRIPTION(rs.getString("DESCRIPTION"));
		s.setTHEME(rs.getInt("THEME"));
		s.setTITLE(rs.getString("TITLE"));
		s.setID(rs.getInt(("ID")));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillPictureUserSettingsFromResultSet(PictureUserSettings, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	public void update(PictureUserSettings bs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(PictureUserSettings) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PictureUserSettingsModel.update"));
		p.setString(1, bs.getDESCRIPTION());
		p.setInt(2, bs.getTHEME());
		p.setString(3, bs.getTITLE());

		p.setInt(4, bs.getID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(PictureUserSettings) - end"); //$NON-NLS-1$
		}
	}

	public void insert(PictureUserSettings bs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("insert(PictureUserSettings) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PictureUserSettingsModel.insert"));
		p.setString(1, bs.getDESCRIPTION());
		p.setInt(2, bs.getTHEME());
		p.setString(3, bs.getTITLE());
		p.setInt(4, bs.getID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("insert(PictureUserSettings) - end"); //$NON-NLS-1$
		}
	}

	public int countAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("countAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("PictureUserSettingsModel.countAll"));
		ResultSet rs = p.executeQuery();
		rs.next();
		int ret = rs.getInt(1);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("countAll() - end"); //$NON-NLS-1$
		}
		return ret;
	}

	public List get(int start, int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("get(int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PictureUserSettingsModel.get"));
		p.setInt(1, start);
		p.setInt(2, limit);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("get(int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			PictureUserSettings u = new PictureUserSettings();
			fillPictureUserSettingsFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

}
